package com.guguskins.back.utils.steam;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Objects;

/**
 * @date 2021/5/10 16:25
 */
@Component
public class SteamUtil {


    private static RestTemplate restTemplate;

//    private static final String KEY = "100E0DA9C607B8402CE77A043B89FD03";

    private static final String CS_GO_APP_ID = "730";

    @Autowired
    public void setRestTemplate(RestTemplate restTemplate) {
        SteamUtil.restTemplate = restTemplate;
    }

    /**
     * 获取用户摘要
     */
    public static JSONArray getPlayerSummaries(String steamIds, String key) {
        String url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/?key=" + key + "&steamids=" + steamIds;
        ResponseEntity<JSONObject> responseEntity = restTemplate.getForEntity(url, JSONObject.class);
        return Objects.requireNonNull(responseEntity.getBody()).getJSONObject("response").getJSONArray("players");
    }

    /**
     * 获取用户游戏列表
     */
    public static JSONObject getOwnedGames(String steamId, String key) {
        String url = "http://api.steampowered.com/IPlayerService/GetOwnedGames/v1/?key=" + key + "&steamid=" + steamId + "&format=json";

        ResponseEntity<JSONObject> responseEntity = restTemplate.getForEntity(url, JSONObject.class);

        return Objects.requireNonNull(responseEntity.getBody()).getJSONObject("response");
    }

    /**
     * 获取封禁记录
     */
    public static String getPlayerBans(String steamIds, String key) {
        String url = "http://api.steampowered.com/ISteamUser/GetPlayerBans/v1/?key=" + key + "&steamids=" + steamIds;
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);

        return Objects.requireNonNull(responseEntity.getBody());
    }

    /**
     * 获取库存
     */
    public static JSONObject getInventory(String steamId) {
        String url = "http://18.223.43.151:8090/test/test2?steamId=" + steamId;
//        String url = "http://steamcommunity.com/inventory/" + steamId + "/730/2?l=schinese&count=5000";

        ResponseEntity<JSONObject> responseEntity = restTemplate.getForEntity(url, JSONObject.class);

        return Objects.requireNonNull(responseEntity.getBody());
    }

    /**
     * 获取交易链接
     */
    public static String getTransactionLink() {
        return "https://steamcommunity.com/id/me/tradeoffers/privacy#trade_offer_access_url";
    }

    /**
     * 获取tag分类
     */
    public static JSONObject getTag() {
        String url = "https://steamcommunity.com/market/appfilters/730?l=schinese";

        ResponseEntity<JSONObject> responseEntity = restTemplate.getForEntity(url, JSONObject.class);

        return responseEntity.getBody();
    }

}
